package com.tony.pmp.server.controller;

import com.google.common.collect.Maps;
import com.tony.pmp.common.response.BaseResponse;
import com.tony.pmp.common.response.StatusCode;
import com.tony.pmp.common.utils.PageUtil;
import com.tony.pmp.common.utils.ValidatorUtil;
import com.tony.pmp.model.entity.SysRoleEntity;
import com.tony.pmp.server.service.SysRoleDeptService;
import com.tony.pmp.server.service.SysRoleMenuService;
import com.tony.pmp.server.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysRoleController
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/25/9:40
 * @Description: 角色接口
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController{

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;

    /**
    * @Description: list 获取角色列表(分页)
    * @Param: [paramMap]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 9:52
    */
    @RequestMapping("/list")
    @RequiresPermissions(value = {"sys:role:list"})
    public BaseResponse list(@RequestParam Map<String,Object> paramMap) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            Map<String, Object> resMap = Maps.newHashMap();
            PageUtil page = sysRoleService.queryPage(paramMap);
            resMap.put("page",page);
            response.setData(resMap);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: save 新增角色信息
    * @Param: [entity, result]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 10:40
    */
    @RequestMapping(value = "/save",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions(value = {"sys:role:save"})
    public BaseResponse save(@RequestBody @Validated SysRoleEntity entity, BindingResult result) {
      String res = ValidatorUtil.checkResult(result);
      if (StringUtils.isNotBlank(res)) {
          return new BaseResponse(StatusCode.InvalidParams.getCode(),res);
      }
        BaseResponse response = new BaseResponse(StatusCode.Success);
      try {
          log.info("新增角色的信息为：{}",entity);
          log.info("新增角色的结果为：{}",sysRoleService.saveRole(entity));

      } catch (Exception e) {
          response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
      }
      return response;
    }

    /**
    * @Description: info 获取角色详情
    * @Param: [id]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 15:04
    */
    @RequestMapping("/info/{id}")
    @RequiresPermissions(value = {"sys:role:info"})
    public BaseResponse info(@PathVariable Long id) {
        if (id == null || id <= 0) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        try {
            SysRoleEntity role = sysRoleService.getById(id);
            //获取角色菜单列表
            List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(id);
            role.setMenuIdList(menuIdList);
            //获取角色部门列表
            List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(id);
            role.setDeptIdList(deptIdList);
            resMap.put("role",role);

        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: update 更新角色信息
    * @Param: [entity, result]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 15:28
    */
    @RequestMapping(value = "/update",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions(value = {"sys:role:update"})
    public BaseResponse update(@RequestBody @Validated SysRoleEntity entity,BindingResult result) {
        String res = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(res)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),res);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("修改角色的信息为：{}",entity);
            log.info("修改角色的状态为：{}",sysRoleService.updateRole(entity));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: delete 删除角色
    * @Param: [ids]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 16:19
    */
    @RequestMapping(value = "/delete",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequiresPermissions(value = {"sys:role:delete"})
    public BaseResponse delete(@RequestBody Long[] ids) {
        if (ids == null || ids.length <= 0) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
          BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("删除角色的id为：{}",ids);
            log.info("删除角色的状态为：{}",sysRoleService.deleBatch(ids));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: select 获取角色列表
    * @Param: []
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/26 14:53
    */
    @RequestMapping("/select")
    public BaseResponse select() {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info(">>>>>>开始获取角色列表信息>>>>>>");
            Map<String,Object> resMap = Maps.newHashMap();
            resMap.put("list",sysRoleService.list());
            response.setData(resMap);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }




















}
